/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;
import model.Obat;
import model.ObatQuery;
import model.HistoryOrder;
import model.HistoryOrderQuery;

  /**
   *
   * @author atikahSH
   *
   */
public class OrderLogic {

    HttpServletRequest request;

    public OrderLogic(HttpServletRequest request) {
        this.request = request;
    }
    /* method untuk melakukan fungsi tambahHistoryOrder, yaitu mengambil nilai textfield pada form order
     * untuk kemudian dimasukkan sebagai nilai untuk membuat baris baru pada tabel orders
     */

    public String tambahHistoryOrder() {
        Obat obat = new Obat();
        List<Obat> obats = new ArrayList<Obat>();
        ObatQuery eq = new ObatQuery();
        List<HistoryOrder> historyOrders = new ArrayList<HistoryOrder>();
        HistoryOrder historyOrder = new HistoryOrder();
        HistoryOrderQuery hpq = new HistoryOrderQuery();
        HttpSession session = request.getSession();

        String namaOrder = request.getParameter("namaOrder");
        String nomerPO = request.getParameter("nomerPO");
        String temp = request.getParameter("jumlahObat");
        String temp2 = request.getParameter("totalOrder");
        String tanggalOrder = request.getParameter("tanggalOrder");
        Obat ename = (Obat) session.getAttribute("ename");
        if (namaOrder != null) {
            historyOrder.setIdObat(ename);
            historyOrder.setNamaOrder(namaOrder);
            historyOrder.setNomerPO(nomerPO);
            historyOrder.setTanggalOrder(tanggalOrder);
            session.setAttribute("tempAddHistoryOrder", historyOrder);
        } else {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahOrder.jsp";
        }
        if (temp.equals("") || temp2.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahOrder.jsp";
        } else {
            try {
                int jumlahObat = Integer.parseInt(temp);
                historyOrder.setJumlahObat(jumlahObat);
            } catch (NumberFormatException nf) {
                session.setAttribute("error", "format jumlah obat harus angka");
                return "tambahOrder.jsp";
            }
            try {
                int totalOrder = Integer.parseInt(temp2);
                historyOrder.setTotalOrder(totalOrder);
            } catch (NumberFormatException nfe) {
                session.setAttribute("error", "format total pembayaran harus angka");
                return "tambahOrder.jsp";
            }
        }
        session.setAttribute("tempAddHistoryOrder", historyOrder);
        if (namaOrder.equals("") || nomerPO.equals("")
                || tanggalOrder.equals("")) {
            session.setAttribute("error", "masih ada field kosong");
            return "tambahOrder.jsp";
        } else {
            try {
                DateFormat formatter;
                Date date;
                formatter = new SimpleDateFormat("dd-MM-yyyy");
                date = (Date) formatter.parse(tanggalOrder);
            } catch (ParseException e) {
                session.setAttribute("error", "tanggal salah, gunakan datepicker");
                return "tambahOrder.jsp";
            }
            hpq.createHistoryOrder(historyOrder);
            session.removeAttribute("tempAddHistoryOrder");
            return "lihatObatPublic.jsp";
        }
    }
    /* method untuk menghapus order berdasarkan id dari order bersangkutan
     */
    public String lihatOrder() {
        HttpSession session = request.getSession();
        HistoryOrder historyOrder = new HistoryOrder();
        List<HistoryOrder> historyOrders = new ArrayList<HistoryOrder>();
        HistoryOrderQuery eq = new HistoryOrderQuery();
        if (session.getAttribute("uname") != null) {
            String temp = request.getParameter("idOrder");
            int idOrder = Integer.parseInt(temp);
            boolean hasilCheck = eq.checkHistoryOrder(idOrder);
            if (hasilCheck) {
                historyOrder = eq.getOrder(idOrder);
                session.setAttribute("ename", historyOrder);
                HistoryOrder ename = (HistoryOrder) session.getAttribute("ename");
                return "lihatOrder.jsp";
            } else {
                session.setAttribute("error", "nama obat tidak ada");
                historyOrders = eq.getLimitedOrders();
                request.setAttribute("historyOrders", historyOrders);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
                return "index.jsp";
            }
        } else {
            String temp = request.getParameter("idOrder");
            int idOrder = Integer.parseInt(temp);
            boolean hasilCheck = eq.checkHistoryOrder(idOrder);
            if (hasilCheck) {
                historyOrder = eq.getOrder(idOrder);
                session.setAttribute("ename", historyOrder);
                HistoryOrder ename = (HistoryOrder) session.getAttribute("ename");
                return "lihatOrder.jsp";
            } else {
                session.setAttribute("error", "nama obat tidak ada");
                historyOrders = eq.getLimitedOrders();
                request.setAttribute("historyOrders", historyOrders);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
                return "index.jsp";
            }
        }
    }

    public String hapusHistoryOrder() throws NonexistentEntityException {
        HttpSession session = request.getSession();
        String idOrder = request.getParameter("idOrder");
        List<HistoryOrder> historyOrders = new ArrayList<HistoryOrder>();
        HistoryOrderQuery hpq = new HistoryOrderQuery();
        List<Obat> obats = new ArrayList<Obat>();
        ObatQuery eq = new ObatQuery();
        if (session.getAttribute("uname") != null) {
            if (idOrder == null) {
                Obat obat = (Obat) session.getAttribute("ename");
                historyOrders = hpq.getHistoryOrders(obat);
                request.setAttribute("historyOrders", historyOrders);
            } else {
                int id = Integer.parseInt(idOrder);
                hpq.deleteHistoryOrder(id);
                Obat obat = (Obat) session.getAttribute("ename");
                historyOrders = hpq.getHistoryOrders(obat);
                request.setAttribute("historyOrders", historyOrders);
            }
            return "lihatOrder.jsp";
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            obats = eq.getLimitedObats();
            request.setAttribute("obats", obats);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Obats</a>");
            return "index.jsp";
        }
    }
    public String ubahOrder() {
        HttpSession session = request.getSession();
        HistoryOrder HistoryOrder = new HistoryOrder();
        List<HistoryOrder> orders = new ArrayList<HistoryOrder>();
        HistoryOrderQuery eq = new HistoryOrderQuery();

        if (session.getAttribute("uname") != null) {
            HistoryOrder ename = (HistoryOrder) session.getAttribute("ename");
            boolean hasilCheck = eq.checkOrder(ename.getNamaOrder());
            if (hasilCheck) {
                HistoryOrder = ename;
                session.setAttribute("tempEditOrder", HistoryOrder);
                return "editOrder.jsp";
            } else {
                session.setAttribute("error", "nama order tidak ada");
                orders = eq.getLimitedOrders();
                request.setAttribute("orders", orders);
                session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Orders</a>");
                return "index.jsp";
            }
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            orders = eq.getLimitedOrders();
            request.setAttribute("orders", orders);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Orders</a>");
            return "index.jsp";
        }
    }


    /* method untuk menyimpan perubahan data order
     * yaitu mengupdate nilai lama dengan nilai yang baru saja dimasukkan
     * saat mengisi form pada halaman editOrder
     */
    public String simpanUbahOrder() throws ParseException {
        HttpSession session = request.getSession();
        HistoryOrder HistoryOrder = new HistoryOrder();
        List<HistoryOrder> orders = new ArrayList<HistoryOrder>();
        HistoryOrderQuery eq = new HistoryOrderQuery();

        if (session.getAttribute("uname") != null) {
            String namaOrder = request.getParameter("namaOrder");
            String jo = request.getParameter("jumlahObat");
            String tanggal = request.getParameter("tanggal");
            String totalOrder = request.getParameter("totalOrder");
            HistoryOrder = (HistoryOrder) session.getAttribute("ename");
            String tempEname = HistoryOrder.getNamaOrder();
            if (namaOrder != null) {
                HistoryOrder.setNamaOrder(namaOrder);
                session.setAttribute("tempEditOrder", HistoryOrder);
            } else {
                session.setAttribute("error", "masih ada field kosong");
                return "editOrder.jsp";
            }

            int jumlahObat = Integer.parseInt(jo);
            HistoryOrder.setJumlahObat(jumlahObat);
            session.setAttribute("tempEditOrder", HistoryOrder);

            if (namaOrder.equals("") || tanggal.equals("") || totalOrder.equals("")) {
                session.setAttribute("error", "masih ada field kosong");
                return "editOrder.jsp";
            } else {
                try {
                    int rek = Integer.parseInt(totalOrder);
                } catch (NumberFormatException n) {
                    session.setAttribute("error", "format kuantitas harus angka");
                    return "editOrder.jsp";
                }
                try {
                    DateFormat formatter;
                    Date date;
                    formatter = new SimpleDateFormat("dd-MM-yyyy");
                    date = (Date) formatter.parse(tanggal);
                } catch (ParseException e) {
                    session.setAttribute("error", "tanggal salah, gunakan datepicker");
                    return "editOrder.jsp";
                }
                boolean hasilCheck = eq.checkOrder(namaOrder);
                if (!hasilCheck || namaOrder.equals(tempEname)) {
                    session.setAttribute("ename", HistoryOrder);
                    eq.editOrder(HistoryOrder);
                    session.removeAttribute("tempEditOrder");
                    return "lihatOrder.jsp";
                } else {
                    session.setAttribute("error", "username sudah terpakai");
                    return "editOrder.jsp";
                }
            }
        } else {
            session.setAttribute("error", "sudah logout sebelumnya");
            orders = eq.getLimitedOrders();
            request.setAttribute("orders", orders);
            session.setAttribute("show", "<a href='account?page=home&&show=all'>Show All Orders</a>");
            return "index.jsp";
        }
    }

}
